import { Util } from "./util.js";
import * as Common from './common.js'
var url = Common.UrlBase + Common.ApiEnums.UserMgr;

export class DigTreasure
{
    static overCounts = 0;
    static counts = 100;
    static user_id = $.cookie('user_id');
    static baoTypes = ["银两", "金币", "书籍", "银两", "银两", "原石", "宝物"];
    static wakong = ["挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空"]
    static wakong2 = ["挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空", "挖空"]
    static isauto = false;
    static goodsList = [];
    static realWa = DigTreasure.baoTypes.concat(DigTreasure.wakong);
    static realWa2 = DigTreasure.baoTypes.concat(DigTreasure.wakong2);
    static click_back()
    {
        window.location.href = "home.html";
    }

    static wbStep(auto, wbBtn)
    {
        var baoIndex = 0;

        if (auto)
        {
            baoIndex = Util.generateRandomInteger(0, DigTreasure.realWa2.length - 1);
            wbBtn.innerText = DigTreasure.realWa2[baoIndex];

        } else
        {
            baoIndex = Util.generateRandomInteger(0, DigTreasure.realWa.length - 1);
            wbBtn.innerText = DigTreasure.realWa[baoIndex];

        }
        wbBtn.disabled = true;
        if (baoIndex >= 7)
        {
            wbBtn.classList.add("btnGray");
        }
        else
        {
            wbBtn.classList.add("btnRed");
            var msgDiv = document.getElementById("msg");
            var newnode = $("<p>" + Util.mytime() + "&nbsp<span class='badge text-bg-danger'>" + Util.getKvFromCookie("name") + "</span>挖到" + Util.getOutStr(baoIndex) + "</p>")
            let firstChild = msgDiv.firstChild;
            $(firstChild).before(newnode);
        }
        DigTreasure.overCounts++;
        if (DigTreasure.overCounts == 9)
        {
            if (DigTreasure.counts == 0)
            {
                document.getElementById("tips").innerText = "没有体力挖不了";
                document.getElementById("wbnext").disabled = true;
            } else
            {
                if (DigTreasure.isauto == false)
                    document.getElementById("wbnext").disabled = false;

            }
        }
    }
    static wbClicked(e)
    {
        var wbBtn = e.currentTarget;
        wbStep(false, wbBtn);
    }
    static wbNextFunc()
    {
        var wbBtns = document.getElementsByClassName("wb");


        if (DigTreasure.overCounts == 9)
        {
            DigTreasure.overCounts = 0;
            DigTreasure.counts--;
        }
        for (var i = DigTreasure.overCounts; i < wbBtns.length; i++)
        {

            wbBtns[i].innerText = "挖宝";
            wbBtns[i].className = "wb btn btn-primary";
            wbBtns[i].disabled = false;
        }
        document.getElementById("tips").innerText = "体力:" + DigTreasure.counts;
        //update
        $.post(
            url,
            {
                "action": "updateUserKvByid",
                "user_id": DigTreasure.user_id,
                "key": "phystr",
                "val": DigTreasure.counts,
                "val_type": 0
            }, function (res)
        {
            //console.log("update coin and tael ok");
            Util.updateCookieFromNet();
        }
        );
    }
    static wbNextBtnClick()
    {

        document.getElementById("wbnext").disabled = true;
        wbNextFunc();


    }



    static onLoad()
    {
        var wbBtns = document.getElementsByClassName("wb");
        var wbnext = document.getElementById("wbnext");
        wbnext.addEventListener('click', DigTreasure.wbNextBtnClick, false);
        for (var i = 0; i < wbBtns.length; i++)
        {
            //console.log(wbBtns[i]);
            wbBtns[i].addEventListener('click', DigTreasure.wbClicked, false);

        }
        DigTreasure.counts = parseInt(Util.getKvFromCookie("phystr"));
        if (DigTreasure.counts == 0)
        {
            document.getElementById("tips").innerText = "体力用完不能挖";
            document.getElementById("wbnext").disabled = true;

        } else
        {
            document.getElementById("tips").innerText = "体力：" + DigTreasure.counts;
            document.getElementById("wbnext").disabled = false;

        }
    }
    static onReady()
    {
        if ($.cookie("user_id") == null)
        {
            alert("请登录!");
            window.location.href = "index.html";
            return;
        }
        $("#wbauto").on('click', function ()
        {
            DigTreasure.btnFunc('wbauto');
        });
    }
    static btnFunc(str)
    {
        var user_id = $.cookie('user_id');
        if (str == 'wbauto' && DigTreasure.overCounts < 9)
        {
            var btns = $("td[scope='wb']>button");
            var thisbtn = document.getElementById(str);
            DigTreasure.isauto = DigTreasure.isauto ? false : true;

            if (DigTreasure.isauto)
            {
                fn(btns);
                thisbtn.innerText = "关闭自动挖宝";
                document.getElementById("wbnext").disabled = true;


            } else
            {
                thisbtn.innerText = "自动挖宝(奖励减半)";
                document.getElementById("wbnext").disabled = false;

            }



        }
    }
}

window.onload = function ()
{
    // DigTreasure.onLoad();

}



$(document).ready(function ()
{
    // DigTreasure.onReady();

});


async function fn(btns)
{

    for (let index = DigTreasure.overCounts; index < btns.length; index++)
    {
        await (function ()
        {
            return new Promise(function (res, rej)
            {
                setTimeout(function ()
                {
                    if (DigTreasure.isauto)
                    {
                        DigTreasure.wbStep(true, btns[index]);
                        res();

                    }
                    else
                    {
                        return rej("auto false")
                    }


                }, 1000)
            }).catch(err =>
            {
                console.log(err);
                return new Promise(() => { })
            })
        }());

    }
    if (DigTreasure.isauto)
    {
        await (function ()
        {
            return new Promise(function (res, rej)
            {
                setTimeout(function ()
                {
                    if (DigTreasure.counts >= 0)
                    {
                        DigTreasure.wbNextFunc();
                        fn($("td[scope='wb']>button"));
                    }
                }, 2000)
            })
        }());
    }


}


